/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package unplag.core;

import java.io.File;
import java.io.FilenameFilter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

/**
 *
 * @author Administrator
 */
public class BaseDir {
    Vector<Work> works;
    String root;
    List<FileFormat> formats=new Vector<FileFormat>();
    ProgressListener pl;
    Vector<TargetFile> filelist;

    public BaseDir(String root){
        this.root=root;
        File f=new File(root);
        File[] fa=f.listFiles();
        works=new Vector<Work>();
        for(int i=0;i<fa.length;i++){
            if(!fa[i].isDirectory())continue;
            works.add(new Work(this,fa[i]));
        }
    }
    public void addFileFormat(FileFormat f){
        formats.add(f);
    }
    public void setProgressListener(ProgressListener pl){
        this.pl=pl;
    }
    private void reportProgress(int n){
        if(pl!=null)
            pl.report(n);
    }
    public int getWorkCount(){
        return works.size();
    }
    public Work getWorkAt(int n){
        return works.get(n);
    }
    public Work getWorkByName(String name){
        for(Work w:works){
            if(w.getName().equals(name))
                return w;
        }
        return null;
    }
    public Work[] getWorks(){
        return works.toArray(new Work[0]);
    }
    public boolean removeWork(Work work){
        return works.remove(work);
    }

    public void scanFile(){
        filelist=new Vector<TargetFile>();
        int count=works.size();
        int th=count/2-1;
        Map<String,Integer> map = new HashMap<String,Integer>();

        for(int i=0;i<count;i++){
            //System.out.println(w.getFile().getName());
            File f=works.get(i).getFile();
            scanDir(f,map);
            reportProgress((i+1)*100/count);
        }

        for(String s:map.keySet()){
            if(map.get(s)>th){
                //sourcelist.add(new SourceFile());
                filelist.add(new TargetFile(s));
            }
        }
    }

    public Vector<TargetFile> getTargetFiles(){
        return (Vector<TargetFile>)filelist.clone();
    }
    
    private void scanDir(File dir,Map<String,Integer> result){
        for(File file:dir.listFiles()){
            if(file.isDirectory()){
                scanDir(file,result);
            }else{
                for(FileFormat format:formats){
                    if(format.accept(file)){
                        String name=file.getPath().substring(root.length()).split("\\\\", 3)[2];
                        if(result.containsKey(name)){
                            result.put(name, result.get(name)+1);
                        }else{
                            result.put(name, 1);
                        }
                        //System.out.println(name);
                        continue;
                    }
                }
            }
        }
    }
}
